//index.js
const apiUrl = require('../../../../config.js').apiUrl
//获取应用实例
var app = getApp();

Page({
  data: {
    subject: 'kemu1',//题目类型
    type: 'mnks',//题目类型
    difficulty:'中等',
    array: ['简单', '中等', '难'],
    switchChecked: true,
    znzj: 1,
    index: 1,
    mid: 10001,
    flag:false
  },

  bindPickerChange:function(e){
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      difficulty: this.data.array[e.detail.value],
      index: e.detail.value
    })
  },

  examstory:function(){
    wx.navigateTo({
      url: '/pages/app/answer/answer_simulate_history/simulate_history?subject=kemu1&type=lsks',
    })
  },

  switch1Change:function(e){
    // console.log('switch',e.detail)
    this.setData({
      switchChecked: e.detail.value
    })
    this.data.znzj = Number(this.data.switchChecked);
    if (this.data.switchChecked == false){
      this.setData({
        flag: true,
        difficulty: '难度随机'
      })
    }else{
      this.setData({
        flag: false,
        difficulty: '中等'
      })
    }
    // console.log('switch', this.data.znzj)
  },

  startExam:function(){
    let current_course_id = wx.getStorageSync('pingshifen_current_course_id');
    var midIndex = 0;
      wx.request({
        url: apiUrl,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + app.globalData.sessionKey
        },
        data: {
          method: 'pingshifen.question.get_mid',
          course_id: current_course_id,
          openid: wx.getStorageSync('openid'),
          difficulty: this.data.index || 0,
          znzj: this.data.znzj
        },
        method: 'POST',
        success: res => {
          console.log('mid', res)
          res.data.data.forEach(function (v, i){
            if(v.score > 0){
              midIndex = i+1;
            }
            console.log('midIndex', midIndex)
          });
          this.setData({
            mid: res.data.data[midIndex].mid
          })
          wx.redirectTo({
            url: `/pages/app/answer/answer_simulate_info/simulate_info?subject=kemu1&mid=${this.data.mid}&znzj=${this.data.znzj}&diffi=${this.data.index}`,
          })
        }
      })
  },

  onLoad(params) {
    var that = this;
    this.data.subject = params.subject;
    this.data.type = params.type;
    that.data.hasUserInfo = true;
    that.data.userInfo = app.globalData.userInfo;
    that.setData(that.data)
  }
});